package com.kiragame.couplescreen;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class EditContactDialogFragment extends DialogFragment {
	private EditText tvName;
	private EditText tvId;
	private Button btnOk;
	private Button btnCancel;
	private Button btnDelete;
	private EditContactDialogListener listener;
	public static String CONTACT_NAME = "CONTACT_NAME";
	private String contactName;

	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.edit_contact, container);
		findViewIds(view);
		init();
		setEvent();
		return view;
	}

	private void init() {
		getDialog().setTitle("Edit Contact");
//		getDialog().getWindow().setSoftInputMode(
//                LayoutParams.SOFT_INPUT_STATE_VISIBLE);
		contactName = getArguments().getString(CONTACT_NAME);
		tvName.setText(contactName);
		tvId.setText(DataManager.getInstance(getActivity()).getAllContact().get(contactName));
	}

	private void setEvent() {
		btnOk.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(listener != null){
					String name = tvName.getText().toString();
					String id = tvId.getText().toString();
					if(name.trim().length() < 0 || id.trim().length() <5){
						Toast.makeText(getActivity(), "Please type Name or Id!", Toast.LENGTH_LONG).show();
						return;
					}
					listener.onOKClickListener(name, id);
				}
			}
		});
		btnCancel.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				dismiss();
			}
		});
		
		btnDelete.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(listener != null){
					listener.onDelete(contactName);
				}
			}
		});
	}

	private void findViewIds(View view) {
		tvName = (EditText) view.findViewById(R.id.tv_Name);
		tvId = (EditText) view.findViewById(R.id.tv_id);
		btnOk = (Button) view.findViewById(R.id.btn_ok);
		btnCancel = (Button) view.findViewById(R.id.btn_cancel);
		btnDelete = (Button)view.findViewById( R.id.btn_delete );
	}
	public interface EditContactDialogListener{
		void onOKClickListener(String name, String id);
		void onCancelListener();
		void onDelete(String name);
	}
	public void setOnAddContactDialogListener(EditContactDialogListener listener){
		this.listener = listener;
	}
}
